<?php 
MYTCONN();

  $prt_HTML = '';
  $tprt2_HTML = '';

  $FlCond = '';
  $Origen = '';
  $id = '';

  $qr = '';
	$CurrLevel = 0;
  $o = 1;
	$dspl1 = '';
	$dspl2 = '';

	if (isset($_GET['DsplMode'])) {
	echo '<div class="cellpadding:3">Conferencia de Compliance</div>';
	$dspl1 = 'style="display:none"'; $dspl2 = 'style="font-size: 8px"';}
  $cor = '#EBEFE9';
  if ($_POST['ccq_level'] == 0) {$itcount=2;} else {$itcount=1;}
  for($g=1;$g<=$itcount;$g++) {
	if ($_POST['ccq_level'] == 0) {$CurrLevel = $g;} else {$CurrLevel = $_POST['ccq_level'];}
  if ($_POST['SRCID'] == 20) { $grpClause = 'SUM(FraEnviado-FraProm)'; }
  if ($_POST['SRCID'] == 21) { $grpClause = 'SUM(FraEnviado-FraProm)'; }
  if ($_POST['SRCID'] == 22) { $grpClause = 'SUM(FraEnviado-FraProm)'; }
  if ($_POST['SRCID'] == 23) { $grpClause = 'SUM(FraEnviado-FraProm)'; }
  if ($_POST['SRCID'] == 25) { $grpClause = 'COUNT(FraID)'; }
  if ($_POST['SRCID'] == 26) { $grpClause = 'COUNT(DISTINCT FraCliNumero)'; }
  if ($_POST['SRCID'] == 27) { $grpClause = 'COUNT(DISTINCT FraCliNumero)'; }

  if ($_POST['ccq_elements'] == 'BnfCedula') {
    $grpField = 'BnfNombre';
    $grpCond = 'BnfCedula';
    $grpLabel = 'Beneficiario';
  } elseif ($_POST['ccq_elements'] == 'FraCtaBenef') {
    $grpField = 'cb_Cuenta';
    $grpCond = 'cb_Banco,cb_Cuenta';
    $grpLabel = 'Conta Bancaria';
  } elseif ($_POST['ccq_elements'] == 'FraCliNumero') {
    $grpField = 'CliNombre';
    $grpCond = 'FraCliNumero';
    $grpLabel = 'Cliente';
  }

  if (isset($_POST['QOrigen'])) {$Origen = 'FraOrigen0 = '.$_POST['QOrigen'];}

  if(isset($_GET['UnpackType'])) {
    switch($_GET['UnpackType']) {
        case 'COUNTRY':		$grpCond = 'BnfPais';
        case 'SENDERS':		$grpCond = 'FraCliNumero';
        case 'RECEIVERS':	$grpCond = 'FraBnfNumero';
        case 'ACCOUNTS':	$grpCond = 'FraCtaBenef';
    }
  }

  $period = " AND FraFechaIngreso BETWEEN '{$_POST['TimeStart']}' AND '{$_POST['TimeEnd']}'";
  $value = $_POST['SRCVALUE'];

  if ($_POST['ccq_type'] == 1) {
    if ($CurrLevel== 1) {
      $llabel = ' Diario';
      $value = 1900;
      $getdate = explode('-',$_POST['PeriodDate']);
      $period = " AND MONTH(FraFechaIngreso) = {$getdate[1]} AND YEAR(FraFechaIngreso) = {$getdate[0]}";
      $grpClause = 'MAX(FraEnviado-FraProm+FraCargo)';
    }
    if ($CurrLevel== 2) {
      $llabel = ' Mensal';
      $value = 3000;
      $getdate = explode('-',$_POST['PeriodDate']);
      $period = " AND MONTH(FraFechaIngreso) = {$getdate[1]} AND YEAR(FraFechaIngreso) = {$getdate[0]}";
      $grpClause = 'SUM(FraEnviado-FraProm+FraCargo)';
    }
    if ($CurrLevel== 3) {
      $llabel = 'Anual';
      $value = 12500;
      $getdate = explode('-',$_POST['PeriodDate']);
      $period = " AND YEAR(FraFechaIngreso) = {$getdate[0]}";
      $grpClause = 'SUM(FraEnviado-FraProm+FraCargo)';
    }
  }

  $having = " HAVING  COL5 > {$value}";
  if(isset($_GET['ID'])) {
    $id = ' AND '.$_POST['ccq_elements'].' = '.$_GET['ID'];
    $having = '';
    $cor = '#EBEFF9';
  }
	$GETMIN = $GLOBALS['MYT']->db_array("SELECT FraID AS QR FROM facturas 
		WHERE YEAR(FraFechaIngreso) = YEAR('{$_POST['TimeStart']}') AND MONTH(FraFechaIngreso) = MONTH('{$_POST['TimeStart']}') ORDER BY FraID ASC LIMIT 0,1");
	$GETMAX = $GLOBALS['MYT']->db_array("SELECT FraID AS QR FROM facturas 
		WHERE YEAR(FraFechaIngreso) = YEAR('{$_POST['TimeEnd']}') AND MONTH(FraFechaIngreso) = MONTH('{$_POST['TimeEnd']}') ORDER BY FraID DESC LIMIT 0,1");
	
	
  echo $qr .= "SELECT
        '{$llabel}' AS Level,
        {$_POST['ccq_elements']} AS O_ID,
        {$grpField} COL2,
        COUNT(FraID) AS COL4,
        {$grpClause} AS COL5,
        COUNT(DISTINCT FraCliNumero) AS COL7,
        COUNT(DISTINCT BnfCedula) AS COL8,
        COUNT(DISTINCT FraCtaBenef) AS COL9,
        COUNT(DISTINCT BnfPais) AS COL10
        FROM Facturas IGNORE INDEX (IdxCli,FraConta)
      INNER JOIN clientes ON FraCliNumero = CliNumero
      INNER JOIN beneficiarios ON FraBnfNumero = BnfNumero
      INNER JOIN hd_paises ON BnfPais = pai_CodElite
      WHERE FraID BETWEEN {$GETMIN} AND {$GETMAX} AND FraStatus NOT IN (1,9,10,21)
      {$period}
      {$id}
      {$Origen}
      GROUP BY {$grpCond}
        {$having}";
    if ($g<$itcount) { $qr .= " UNION ";} else { $qr .= " ORDER BY COL2, Level";}
  }
// $GET = $GLOBALS['MYT']->db_array($qr);
  $CARD = $GET[0];
  $getElemStr = '';
  for ($f=1;$f<=$CARD;$f++) {
    $getElemStr .= $GET[$f]['O_ID'].',';
  }
  
    $GETDetls = $GLOBALS['MYT']->db_array("SELECT
        {$_POST['ccq_elements']} AS O_ID,
        BnfNombre AS COL1,
        CliNombre COL2,
       (FraEnviado-FraProm+FraCargo) AS COL5,
        FraOrigen0 AS COL11,
        FraReferenciaAg AS COL12,
        DATE(FraFechaIngreso) AS COL13,
        FraARecibir AS COL14,
        FraTasaAgn AS COL15,
        name AS COL16
      FROM Facturas IGNORE INDEX (IdxCli,FraConta)
      INNER JOIN clientes ON FraCliNumero = CliNumero
      INNER JOIN beneficiarios ON FraBnfNumero = BnfNumero
      INNER JOIN systemoffice.main_users ON ref = FraOrigen2
      WHERE FraStatus NOT IN (1,9,10,21)
      {$period}
      {$id}
      {$Origen}
      AND {$_POST['ccq_elements']} IN (".substr($getElemStr,0,strlen($getElemStr)-1).")
      ORDER BY {$grpField},FraFechaIngreso");
    $CDT = $GETDetls[0];
    $c = 1;

if ($CARD > 2000) {
$prt_HTML .= '<script charset="utf-8" type="text/javascript">alert("A consulta devolveu demasiados resultados, redefina a consulta.");</script>';
} else {
  $prt_HTML .= '<table '.$dspl2.' bgcolor="'.$cor.'" cellspacing="2" width="98%">
  <tr><td><img id="ico" src="'.$GLOBALS['GFX'].'Objects/Tree/plus.gif" '.$dspl1.' onclick="toggleall('.$CARD.');"></td>
      <td NOWRAP>Nivel</td>
      <td NOWRAP>Data</td>
      <td>Dominio</td>
      <td>Remessa</td>
      <td>Cliente</td>
      <td>Beneficiario</td>
      <td>Valor Pago</td>
      <td>Cambio</td>
      <td>Valor Enviado</td>
      <td>Caixa</td></tr>';
  for ($f=1;$f<=$CARD;$f++) {
  	if ($f>1 && $GET[$f]['O_ID'] != $GET[$f-1]['O_ID']) { $o++; $prt_HTML .= $tprt_HTML.$tprt2_HTML; $tprt2_HTML=''; } 
    $p = $c;
    $tprt_HTML = '<tr><td colspan="15"><hr width="100%" size="1" /></td></tr>
      <tr><td><img id="ico'.$o.'" src="'.$GLOBALS['GFX'].'Objects/Tree/plus.gif" '.$dspl1.' onclick="toggle('.$o.');">'.$o.'</td>
      <td NOWRAP>'.$GET[$f]['Level'].'</td>
      <td NOWRAP></td>
      <td NOWRAP></td>
      <td NOWRAP><b>#'.$GET[$f]['COL4'].'</b></td>
      <td NOWRAP><b>'.$GET[$f]['COL2'].'</b></td>
      <td NOWRAP width="250"></td>
      <td NOWRAP></td>
      <td NOWRAP></td>
      <td NOWRAP align="right"><b>'.number_format($GET[$f]['COL5'],2).'</b></td>
      <td NOWRAP width="100"></td></tr>
      <div >';
    for ($b=$p;$b<=$CDT;$b++) {
      if ($GETDetls[$b]['O_ID'] != $GET[$f]['O_ID']) { $b = $CDT+1;} else {
      $c++;
      $tprt2_HTML .= '<tr height="20" bgcolor="#FFFFFF" name="dtl'.$o.'" id="dtl'.$o.'" style="display:block"><td></td>
      <td></td><td NOWRAP>'.$GETDetls[$b]['COL13'].'</td>
        <td NOWRAP>'.$GETDetls[$b]['COL11'].'</td>
        <td NOWRAP>'.$GETDetls[$b]['COL12'].'</td>
        <td NOWRAP>'.$GETDetls[$b]['COL2'].'</td>
        <td NOWRAP>'.$GETDetls[$b]['COL1'].'</td>
        <td NOWRAP align="right">'.number_format($GETDetls[$b]['COL14'],2).'</td>
        <td NOWRAP align="right">'.$GETDetls[$b]['COL15'].'</td>
        <td NOWRAP align="right">'.number_format($GETDetls[$b]['COL5'],2).'</td>
        <td NOWRAP>'.$GETDetls[$b]['COL16'].'</td></tr>';
      }
    }
    $tprt_HTML .= '</div>';
    if ($f==$CARD && $GET[$f]['O_ID'] != $GET[$f-1]['O_ID']) { $o++; $prt_HTML .= $tprt_HTML.$tprt2_HTML; $tprt2_HTML=''; }
    }
    
$prt_HTML .= '</table>';
}
echo $prt_HTML; ?>